
<template>
  <section class="todoapp">
    <!-- 除了驼峰, 还可以使用-转换链接 -->
    <TodoHeader @addTask="addTaskHandler" :list="list"></TodoHeader>
    <TodoMain @delTask="delTaskHandler" :list="showTASK"></TodoMain>
    <TodoFooter :list="showTASK" @showTask="showTaskHandler"
      @clearTask="clearTaskHandler"    
    ></TodoFooter>
  </section>
</template>

<script>
// 1.0 样式引入
/* import "./styles/base.css"
import "./styles/index.css" */
    
import TodoHeader from "./components/TodoHeader";
import TodoMain from "./components/TodoMain";
import TodoFooter from "./components/TodoFooter";


export default {
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  data(){
    return{
      list: JSON.parse(localStorage.getItem('list') || '[]'),
      isAll:'all',
      
    }
  },
  methods:{
    /* 添加 */
    addTaskHandler(taskName){
      let id= this.list.length>0 ? this.list[this.list.length-1].id+1 : 100

      this.list.push({
        id,
        name:taskName,
        isDone:false
      })
    },

    /* 删除 */
    delTaskHandler(id){
        let index= this.list.findIndex(item=>item.id===id)
        this.list.splice(index,1)
    },
    showTaskHandler(isAll){
      /* console.log(isAll); */
      this.isAll=isAll
    },
    clearTaskHandler(){
      this.list=this.list.filter(item=>!item.isDone)
    }
  },
  computed:{
   showTASK(){
     if(this.isAll=='no'){
       return this.list.filter(item=>!item.isDone)
     }else if(this.isAll=='yes'){
       return this.list.filter(item=>item.isDone)
     }else{
       return this.list
     }
   }
  },
  /* 数据缓存 */
  watch:{
    list:{
      deep:true,
      handler(){
        localStorage.setItem('list',JSON.stringify(this.list))
      }
    }
  }
};
</script>